package com.guslegend.component.impl;

import com.guslegend.component.MailService;
import com.guslegend.constant.CacheKey;
import com.guslegend.enums.BizCodeEnum;
import com.guslegend.enums.SendCodeEnum;
import com.guslegend.service.NotifyService;
import com.guslegend.util.CheckUtil;
import com.guslegend.util.CommonUtil;
import com.guslegend.util.JsonData;
import com.guslegend.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;


import java.util.concurrent.TimeUnit;

/**
 * @author: guslegend
 * @description: 图片验证码
 * @create: 2025-05-26 08:25
 **/
@Service
@Slf4j
public class MailServiceImpl implements MailService {

    /**
     * Spring Boot 提供了一个发送邮件的简单抽象，直接注入即可使用
     */
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 配置文件中的发送邮箱
     */
    @Value("${spring.mail.from}")
    private String from;

    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        //创建SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件发送人
        message.setFrom(from);
        //邮件接收人
        message.setTo(to);
        //邮件主题
        message.setSubject(subject);
        //邮件内容
        message.setText(content);
        //发送邮件
        try {
            mailSender.send(message);
            log.info("邮件发成功:{}", message.toString());
        } catch (MailException e) {
            log.error("发送给{}的邮件失败 {}", to, e.getMessage());
            throw e;
        }

    }

}